package com.yanchangapp.ui.presenter;

import com.yanchangapp.base.HeadApiRetrofit;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.model.bean.LookLinkWorkBean;
import com.yanchangapp.model.request.MakePlanManRequest;
import com.yanchangapp.model.request.MakePlanWorkRequest;
import com.yanchangapp.ui.view.LookLinkWorkAtView;
import com.zxl.baselib.bean.response.BaseListResponse;
import com.zxl.baselib.bean.response.BaseResponse;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BaseObserver;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.ui.base.ExceptionHandle;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by apple on 2019-10-12.
 * description: 查看关联工作项P层
 */
public class LookLinkWorkAtPresenter extends BasePresenter<LookLinkWorkAtView> {

    public LookLinkWorkAtPresenter(BaseActivity context) {
        super(context);
    }

    public void queryList(long id){
        HeadApiRetrofit.getInstance().getQueryLookWorkItem(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseListResponse<LookLinkWorkBean>>() {
                    @Override
                    public void onNext(BaseListResponse<LookLinkWorkBean> lookLinkWorkBeanBaseListResponse) {
                        if (null != getView()){
                            if (AppConstant.REQUEST_SUCCESS == lookLinkWorkBeanBaseListResponse.getCode()){
                                getView().queryWorksSuccess(lookLinkWorkBeanBaseListResponse.getData());
                            }else {
                                getView().queryWorksFailure(lookLinkWorkBeanBaseListResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().queryWorksFailure("网络错误");
                        }
                    }
                });
    }



    public void actionMakePlan(MakePlanManRequest makePlanWorkRequest){
        getView().showLoadingDialog();
        HeadApiRetrofit.getInstance().postMakePlanFromMan(makePlanWorkRequest)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        if (getView() != null){
                            getView().hideLoadingDialog();
                            if (baseResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                                getView().allotPlanSuccess("分配工作成功");
                            }else {
                                getView().allotPlanFailure(baseResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().hideLoadingDialog();
                            getView().allotPlanFailure("网络错误");
                        }
                    }
                });
    }
}
